﻿using System.Threading.Tasks;

namespace WhenAll
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"D:\temp");
            Task<int>[] tasks = new Task<int>[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                tasks[i] = ReadCharsCountAsync(files[i]);
            }
            int[] results = await Task.WhenAll(tasks);
            int totalChars = results.Sum();
            Console.WriteLine(totalChars);
        }

        static async Task<int> ReadCharsCountAsync(string filename)
        {
            string s = await File.ReadAllTextAsync(filename);
            return s.Length;
        }
    }
}
